{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# 10 数据连接"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  key  data1\n",
      "0   b      0\n",
      "1   b      3\n",
      "2   a      7\n",
      "3   c      6\n",
      "4   a      5\n",
      "5   a      6\n",
      "6   b      2\n",
      "--------------------------------------------------\n",
      "  key  data2\n",
      "0   a      0\n",
      "1   b      0\n",
      "2   d      2\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "df_obj1 = pd.DataFrame({'key': ['b', 'b', 'a', 'c', 'a', 'a', 'b'],\n",
    "                        'data1' : np.random.randint(0,10,7)})\n",
    "df_obj2 = pd.DataFrame({'key': ['a', 'b' ,'d'],\n",
    "                        'data2' : np.random.randint(0,10,3)})\n",
    "\n",
    "print(df_obj1)\n",
    "print('-'*50)\n",
    "print(df_obj2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-07-08T06:43:14.681378600Z",
     "start_time": "2024-07-08T06:43:12.400563400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "data": {
      "text/plain": "  key  data1  data2\n0   b      0      0\n1   b      3      0\n2   a      7      0\n3   a      5      0\n4   a      6      0\n5   b      2      0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>key</th>\n      <th>data1</th>\n      <th>data2</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>b</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>b</td>\n      <td>3</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>a</td>\n      <td>7</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>a</td>\n      <td>5</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>a</td>\n      <td>6</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>b</td>\n      <td>2</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#默认连接使用相同的列名，连接方式是内连接\n",
    "pd.merge(df_obj1, df_obj2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-07-08T06:43:50.705661500Z",
     "start_time": "2024-07-08T06:43:50.685674900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "  key_x  data1 key_y  data2\n0     b      0     a      0\n1     b      3     b      0\n2     a      7     d      2",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>key_x</th>\n      <th>data1</th>\n      <th>key_y</th>\n      <th>data2</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>b</td>\n      <td>0</td>\n      <td>a</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>b</td>\n      <td>3</td>\n      <td>b</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>a</td>\n      <td>7</td>\n      <td>d</td>\n      <td>2</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#左df和右df都拿索引连接,默认是内连接\n",
    "pd.merge(df_obj1, df_obj2,left_index=True,right_index=True)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-07-08T06:45:00.354540300Z",
     "start_time": "2024-07-08T06:45:00.329390300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "  key  data1  data2\n0   b      0      0\n1   b      3      0\n2   a      7      0\n3   a      5      0\n4   a      6      0\n5   b      2      0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>key</th>\n      <th>data1</th>\n      <th>data2</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>b</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>b</td>\n      <td>3</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>a</td>\n      <td>7</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>a</td>\n      <td>5</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>a</td>\n      <td>6</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>b</td>\n      <td>2</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#左表和右表都拿key列来连接\n",
    "pd.merge(df_obj1, df_obj2, on='key')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-07-08T06:45:27.797860300Z",
     "start_time": "2024-07-08T06:45:27.781352700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [],
   "source": [
    "# 更改列名\n",
    "df_obj1 = df_obj1.rename(columns={'key':'key1'})\n",
    "df_obj2 = df_obj2.rename(columns={'key':'key2'})"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-07-08T06:45:39.888543Z",
     "start_time": "2024-07-08T06:45:39.878848800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "data": {
      "text/plain": "  key1  data1\n0    b      0\n1    b      3\n2    a      7\n3    c      6\n4    a      5\n5    a      6\n6    b      2",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>key1</th>\n      <th>data1</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>b</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>b</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>a</td>\n      <td>7</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>c</td>\n      <td>6</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>a</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>a</td>\n      <td>6</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>b</td>\n      <td>2</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_obj1"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-07-08T06:45:52.900789200Z",
     "start_time": "2024-07-08T06:45:52.890796700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "data": {
      "text/plain": "  key2  data2\n0    a      0\n1    b      0\n2    d      2",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>key2</th>\n      <th>data2</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>a</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>b</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>d</td>\n      <td>2</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_obj2"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-07-08T06:45:57.369877200Z",
     "start_time": "2024-07-08T06:45:57.359572400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "data": {
      "text/plain": "  key1  data1 key2  data2\n0    b      0    b      0\n1    b      3    b      0\n2    a      7    a      0\n3    a      5    a      0\n4    a      6    a      0\n5    b      2    b      0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>key1</th>\n      <th>data1</th>\n      <th>key2</th>\n      <th>data2</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>b</td>\n      <td>0</td>\n      <td>b</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>b</td>\n      <td>3</td>\n      <td>b</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>a</td>\n      <td>7</td>\n      <td>a</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>a</td>\n      <td>5</td>\n      <td>a</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>a</td>\n      <td>6</td>\n      <td>a</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>b</td>\n      <td>2</td>\n      <td>b</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#左表以key1来连接，右表以key2来连接\n",
    "pd.merge(df_obj1, df_obj2, left_on='key1', right_on='key2')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-07-08T06:46:06.777732500Z",
     "start_time": "2024-07-08T06:46:06.766706100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "data": {
      "text/plain": "  key1  data1 key2  data2\n0    a    7.0    a    0.0\n1    a    5.0    a    0.0\n2    a    6.0    a    0.0\n3    b    0.0    b    0.0\n4    b    3.0    b    0.0\n5    b    2.0    b    0.0\n6    c    6.0  NaN    NaN\n7  NaN    NaN    d    2.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>key1</th>\n      <th>data1</th>\n      <th>key2</th>\n      <th>data2</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>a</td>\n      <td>7.0</td>\n      <td>a</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>a</td>\n      <td>5.0</td>\n      <td>a</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>a</td>\n      <td>6.0</td>\n      <td>a</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>b</td>\n      <td>0.0</td>\n      <td>b</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>b</td>\n      <td>3.0</td>\n      <td>b</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>b</td>\n      <td>2.0</td>\n      <td>b</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>c</td>\n      <td>6.0</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>d</td>\n      <td>2.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#全外连接\n",
    "pd.merge(df_obj1, df_obj2, left_on='key1', right_on='key2', how='outer')  #全外连接"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-07-08T06:46:39.496580700Z",
     "start_time": "2024-07-08T06:46:39.471390Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "data": {
      "text/plain": "  key1  data1 key2  data2\n0    b      0    b    0.0\n1    b      3    b    0.0\n2    a      7    a    0.0\n3    c      6  NaN    NaN\n4    a      5    a    0.0\n5    a      6    a    0.0\n6    b      2    b    0.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>key1</th>\n      <th>data1</th>\n      <th>key2</th>\n      <th>data2</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>b</td>\n      <td>0</td>\n      <td>b</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>b</td>\n      <td>3</td>\n      <td>b</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>a</td>\n      <td>7</td>\n      <td>a</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>c</td>\n      <td>6</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>a</td>\n      <td>5</td>\n      <td>a</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>a</td>\n      <td>6</td>\n      <td>a</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>b</td>\n      <td>2</td>\n      <td>b</td>\n      <td>0.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#left join 等价于 left  join\n",
    "pd.merge(df_obj1, df_obj2, left_on='key1', right_on='key2', how='left')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-07-08T06:47:00.539952700Z",
     "start_time": "2024-07-08T06:47:00.524962700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "data": {
      "text/plain": "  key1  data1 key2  data2\n0    a    7.0    a      0\n1    a    5.0    a      0\n2    a    6.0    a      0\n3    b    0.0    b      0\n4    b    3.0    b      0\n5    b    2.0    b      0\n6  NaN    NaN    d      2",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>key1</th>\n      <th>data1</th>\n      <th>key2</th>\n      <th>data2</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>a</td>\n      <td>7.0</td>\n      <td>a</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>a</td>\n      <td>5.0</td>\n      <td>a</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>a</td>\n      <td>6.0</td>\n      <td>a</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>b</td>\n      <td>0.0</td>\n      <td>b</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>b</td>\n      <td>3.0</td>\n      <td>b</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>b</td>\n      <td>2.0</td>\n      <td>b</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>d</td>\n      <td>2</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# right等价于数据库的right join\n",
    "pd.merge(df_obj1, df_obj2, left_on='key1', right_on='key2', how='right')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-07-08T06:47:18.492772600Z",
     "start_time": "2024-07-08T06:47:18.455006300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  key  data_left  data_right\n",
      "0   b          4           0\n",
      "1   b          4           0\n",
      "2   a          6           1\n",
      "3   a          4           1\n",
      "4   a          1           1\n",
      "5   b          6           0\n"
     ]
    }
   ],
   "source": [
    "# 处理重复列名\n",
    "df_obj1 = pd.DataFrame({'key': ['b', 'b', 'a', 'c', 'a', 'a', 'b'],\n",
    "                        'data' : np.random.randint(0,10,7)})\n",
    "df_obj2 = pd.DataFrame({'key': ['a', 'b', 'd'],\n",
    "                        'data' : np.random.randint(0,10,3)})\n",
    "#给相同的数据列添加后缀\n",
    "print(pd.merge(df_obj1, df_obj2, on='key', suffixes=('_left', '_right')))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-07-08T06:47:56.912952900Z",
     "start_time": "2024-07-08T06:47:56.901404400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  key  data1\n",
      "0   b      7\n",
      "1   b      7\n",
      "2   a      4\n",
      "3   c      9\n",
      "4   a      6\n",
      "5   a      8\n",
      "6   b      4\n",
      "   data2\n",
      "a      7\n",
      "b      5\n",
      "d      9\n"
     ]
    }
   ],
   "source": [
    "# 按索引连接\n",
    "df_obj1 = pd.DataFrame({'key': ['b', 'b', 'a', 'c', 'a', 'a', 'b'],\n",
    "                        'data1' : np.random.randint(0,10,7)})\n",
    "df_obj2 = pd.DataFrame({'data2' : np.random.randint(0,10,3)}, index=['a', 'b', 'd'])\n",
    "print(df_obj1)\n",
    "print(df_obj2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-07-08T06:48:23.204848500Z",
     "start_time": "2024-07-08T06:48:23.192643500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  key  data1  data2\n",
      "0   b      7      5\n",
      "1   b      7      5\n",
      "2   a      4      7\n",
      "4   a      6      7\n",
      "5   a      8      7\n",
      "6   b      4      5\n"
     ]
    }
   ],
   "source": [
    "print(pd.merge(df_obj1, df_obj2, left_on='key', right_index=True))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-07-08T06:49:02.168280800Z",
     "start_time": "2024-07-08T06:49:02.142839700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "data": {
      "text/plain": "   data2 key  data1\n2      7   a      4\n4      7   a      6\n5      7   a      8\n0      5   b      7\n1      5   b      7\n6      5   b      4",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>data2</th>\n      <th>key</th>\n      <th>data1</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2</th>\n      <td>7</td>\n      <td>a</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>7</td>\n      <td>a</td>\n      <td>6</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>7</td>\n      <td>a</td>\n      <td>8</td>\n    </tr>\n    <tr>\n      <th>0</th>\n      <td>5</td>\n      <td>b</td>\n      <td>7</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>5</td>\n      <td>b</td>\n      <td>7</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>5</td>\n      <td>b</td>\n      <td>4</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.merge(df_obj2,df_obj1, left_index=True, right_on='key')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-07-08T06:49:43.476183100Z",
     "start_time": "2024-07-08T06:49:43.447039500Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
